package com.example.auth.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.auth.consts.IndexConst;
import com.example.auth.entity.SysMenu;
import com.example.auth.entity.dto.SysMenuCreateDTO;
import com.example.auth.entity.dto.SysMenuEditDTO;
import com.example.auth.entity.vo.RouterVO;
import com.example.auth.entity.vo.SysMenuQueryVO;
import com.example.auth.exception.LyException;
import com.example.auth.mapper.ISysMenuMapper;
import com.example.auth.service.SysMenuService;
import com.example.auth.mapper.SysMenuMapper;
import com.example.auth.utils.MenuHelper;
import com.example.auth.utils.RouterHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author SIE
* @description 针对表【sys_menu(菜单表)】的数据库操作Service实现
* @createDate 2023-07-07 13:05:31
*/
@Service
@Slf4j
public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu>
    implements SysMenuService{

    @Autowired
    private SysMenuMapper sysMenuMapper;

    @Override
    public List<String> findUserPermsList(Long userId) {
        return sysMenuMapper.findUserPermsList(userId);
    }

    @Override
    public List<SysMenu> findNodes() {
        List<SysMenu> sysMenuList = sysMenuMapper.selectList(new LambdaQueryWrapper<SysMenu>().orderByAsc(SysMenu::getSortValue));
        if(CollectionUtils.isEmpty(sysMenuList)){
            return null;
        }
        return MenuHelper.buildTree(sysMenuList);
    }

    @Override
    public void insertMenu(SysMenuCreateDTO sysMenuCreateDTO) {
        if(ObjectUtils.isEmpty(sysMenuCreateDTO)){
            throw new LyException("请求的对象不能为空");
        }
        SysMenu sysMenu = ISysMenuMapper.INSTANCT.sysMenuCreateDTOToSysMenu(sysMenuCreateDTO);
        sysMenuMapper.insert(sysMenu);
    }

    @Override
    public void updateMenu(SysMenuEditDTO sysMenuEditDTO) {
        if(ObjectUtils.isEmpty(sysMenuEditDTO)){
            throw new LyException("请求的对象不能为空");
        }
        SysMenu sysMenu = ISysMenuMapper.INSTANCT.sysMenuEditDTOToSysMenu(sysMenuEditDTO);
        sysMenuMapper.updateById(sysMenu);
    }

    @Override
    public SysMenuQueryVO getMenuById(Long id) {
        SysMenu sysMenu = sysMenuMapper.selectById(id);
        return ISysMenuMapper.INSTANCT.sysMenuToSSysMenuQueryVO(sysMenu);
    }

    @Transactional(rollbackFor = {Throwable.class})
    @Override
    public void removeMenuById(Long id) {
        //删除菜单
        sysMenuMapper.deleteById(id);
        sysMenuMapper.deleteRoleMenuByMenuId(id);
    }

    @Override
    public SysMenuQueryVO getParentMenuByParentId(Long parentId) {
        SysMenu sysMenu = sysMenuMapper.selectById(parentId);
        return ISysMenuMapper.INSTANCT.sysMenuToSSysMenuQueryVO(sysMenu);
    }

    @Override
    public List<RouterVO> getUserMenuList(Long userId) {
        List<SysMenu> sysMenuList = new ArrayList<>();
        if(userId == 1){
            sysMenuList = sysMenuMapper.selectList(new LambdaQueryWrapper<SysMenu>().eq(SysMenu::getStatus, IndexConst.STATUS_OPEN).orderByAsc(SysMenu::getSortValue));
        }else {
            sysMenuList = sysMenuMapper.getListByUserId(userId);
        }
        List<SysMenu> sysMenuTree = MenuHelper.buildTree(sysMenuList);
        return RouterHelper.buildRouters(sysMenuTree);
    }

    @Override
    public List<String> getUserPermsList(Long userId) {
        List<String> userPermsList = new ArrayList<>();
        if(userId == 1){
            List<SysMenu> sysMenuList = sysMenuMapper.selectList(new LambdaQueryWrapper<SysMenu>().eq(SysMenu::getStatus,IndexConst.STATUS_OPEN));
            userPermsList = sysMenuList.stream().map(SysMenu::getPerms).collect(Collectors.toList());
        }else {
            userPermsList = sysMenuMapper.findUserPermsList(userId);
        }
        userPermsList =userPermsList.stream().filter(string -> !StringUtils.isEmpty(string)).collect(Collectors.toList());
        return userPermsList;
    }
}




